package com.servlet;

import com.beans.GoodsInfo;
import com.beans.PageInfo;
import com.dao.GoodsDao;
import com.utils.PageUtil;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@MultipartConfig
@WebServlet("/GoodsServlet.do")
public class GoodsServlet extends HttpServlet {
    GoodsDao goodsDao=new GoodsDao();
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String flag=request.getParameter("flag");
        if("manage".equals(flag)){
            manage(request,response);
        }
        else if("del".equals(flag)){
            del(request,response);
        }
        else if("delAll".equals(flag)){
            delAll(request,response);
        }
        else if("showPicture".equals(flag)){
            showPicture(request,response);
        }
        else if("add".equals(flag)){
         add(request,response);
        }
        else if("update".equals(flag)){
            update(request,response);
        }
    }
    //更新商品
    private void update(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        Integer goodsId=Integer.parseInt(request.getParameter("goodsId")); // 对于要修改的商品,是应该有ID的
        String goodsName = request.getParameter("goodsName");
        Integer bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
        Integer smallCateId=Integer.parseInt(request.getParameter("smallCateId"));
        String unit=request.getParameter("unit");
        float price = Float.parseFloat( request.getParameter("price"));
        String producter=request.getParameter("producter");
        String des = request.getParameter("des");

        GoodsInfo goods=goodsDao.getGoodsById(goodsId);

        goods.setGoodsName(goodsName);
        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        goods.setUnit(unit);
        goods.setPrice(price);
        goods.setProducter(producter);
        goods.setDes(des);

        //处理图片数据
        Part part= request.getPart("picture");
        if(  part.getSize()!=0){
            // 如果用户没有上传图片,	表示用户不想更改原来的图片,否则表示用户要改图片
            InputStream in=part.getInputStream();
            byte [] pictureData=new byte [in.available()];
            in.read(pictureData);
            goods.setPictureData(pictureData);
        }

        if(goodsDao.updateGoods(goods)==1)
        {
            request.setAttribute("msg", "商品信息修改成功!");
            request.setAttribute("goods", goods);  //因为前端要进行数据回显,由于是上传表单,所以前端无法使用 ${param.属性名}的方式
            request.getRequestDispatcher("/goods/goods_edit.jsp").forward(request,response);
        }
    }

    //添加商品
    private void add(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
        String goodsName = request.getParameter("goodsName");
        Integer bigCateId=Integer.parseInt(request.getParameter("bigCateId"));
        Integer smallCateId=Integer.parseInt(request.getParameter("smallCateId"));
        String unit=request.getParameter("unit");
        float price = Float.parseFloat( request.getParameter("price"));
        String producter=request.getParameter("producter");
        String des = request.getParameter("des");

        //把图片对应的输入流,转成字节数组
        Part part = request.getPart("picture");//picture 是上传框(<input type="file" name="picture">) 的name属性
        InputStream in=part.getInputStream();
        byte [] pictureData=new byte[in.available()];
        in.read(pictureData);

        GoodsInfo goods=new GoodsInfo();
        goods.setPictureData(pictureData);
        goods.setGoodsName(goodsName);
        goods.setBigCateId(bigCateId);
        goods.setSmallCateId(smallCateId);
        goods.setUnit(unit);
        goods.setPrice(price);
        goods.setProducter(producter);
        goods.setDes(des);

        //添加商品,并得到添加的商品的id,主要用于在前台回显图片
        int goodsId=goodsDao.addGoods(goods);
        goods.setId(goodsId);

        request.setAttribute("msg", "商品添加成功");
        request.setAttribute("goods", goods);  //因为前端要进行数据回显,由于是图片上传表单,所以前端无法使用 ${param.属性名}的方式
        request.getRequestDispatcher("/goods/goods_add.jsp").forward(request,response);
    }

    //显示图片
    private void showPicture(HttpServletRequest request,HttpServletResponse response) throws IOException {
        int goodsId= Integer.parseInt(request.getParameter("goodsId"));
        GoodsDao dao=new GoodsDao();
        GoodsInfo goods=  dao.getGoodsById(goodsId);
        response.setContentType("image/jpg");

        ServletOutputStream outstream =response.getOutputStream();
        outstream.write(goods.getPictureData());
        outstream.flush();
    }
    //删除多个
    private void delAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String[] ids = request.getParameterValues("ck_id");
        goodsDao.delAdmin(ids);
        request.setAttribute("adminManage", "删除成功");
        manage(request, response);
    }

    //删除商品
    private void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer goodsId= Integer.parseInt(request.getParameter("goodsId"));
        int result=goodsDao.delGoodsById(goodsId);
        if(result==1){
            manage(request,response);
        }
    }
    //商品管理
    private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int bigCateId=request.getParameter("bigCateId")==null?0:Integer.parseInt(request.getParameter("bigCateId"));
        int smallCateId=request.getParameter("smallCateId")==null?0:Integer.parseInt(request.getParameter("smallCateId"));
        int pageIndex=request.getParameter("pageIndex")==null?1:Integer.parseInt(request.getParameter("pageIndex"));
        String goodsName=request.getParameter("goodsName");

        int rowCount=goodsDao.getGoodsCount(bigCateId,smallCateId,goodsName);

        PageInfo pageInfo= PageUtil.getPageInfo(5, rowCount, pageIndex);
        List<GoodsInfo> goodsList= goodsDao.getGoodsList(bigCateId,smallCateId,goodsName,pageInfo);
        request.setAttribute("goodsList", goodsList);
        request.setAttribute("pageInfo", pageInfo);
        request.getRequestDispatcher("/goods/goods_manage.jsp").forward(request, response);

    }
}
